#include<stdio.h>
#include<errno.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<signal.h>
#include<time.h>

#include "cJSON.h"
#include "ds18b20.c"

#define SERVER_IP     "127.0.0.1"
#define SERVER_PORT    8889
#define MSG_STR       "Hello,Unix Network Program World!"

int ds18b20_get_temperature(float *temp);

int socket_connect(char *ip,int port);
int pack_json(char *buf,int size,char *id,float temp,char *timep);

void sig_handler(int signum)
{
    if(signum == SIGPIPE)
    {   
        printf("socket disconnected\n");
    }
}

int get_devid(char *id,int size)
{
    strncpy(id,"rpi#001",size);
}
int get_time(char *timep)
{
    time_t timer;
    struct tm *tblock;
    timer = time(NULL);
    tblock = localtime(&timer);

    strcpy(timep, asctime(tblock));
}

int main(int argc,char **argv)
{
    int                  conn_fd = -1;
    int                  rv = -1;
    char                 buf[1024];
    float                temp;
    int                  connected = 0;
    char                 devid[8];  /*rpi#001*/
    char                 timep[32];


    signal( SIGPIPE,sig_handler);


    while(1)
    {
        memset(buf,0,sizeof(buf));
        if( !connected )
        {
            conn_fd = socket_connect(SERVER_IP, SERVER_PORT);
            if(conn_fd < 0)
            {
                connected = 0;
                sleep(5);
                continue;
            }
            else
            {
                connected = 1;
            }
        }

        /*device ID,time,temperature*/
        get_devid(devid, sizeof(devid));
        get_time(timep);
        ds18b20_get_temperature(&temp);

        pack_json(buf,sizeof(buf),devid,temp,timep);


        printf("JSON String: %s\n", buf);

        if(write(conn_fd,buf,strlen(buf)) <=0)
        {
            printf("Write data to server[%s:%d] failure: %s\n", SERVER_IP, SERVER_PORT,strerror(errno));
            connected = 0;
            continue;
        }
        printf("send over\n");
        sleep(5);

    }
cleanup:
    close(conn_fd);
}
int pack_json(char *buf,int size,char *id,float temp,char *timep)
{
    cJSON *root = NULL;
    if(!buf || size<=0 || !id)
    {
        printf("Invalid input arguments\n");
        return -1;
    }

    root = cJSON_CreateObject();

    cJSON_AddItemToObject(root, "id",cJSON_CreateString(id));
    cJSON_AddStringToObject(root,"time",timep);
    cJSON_AddNumberToObject(root,"temperature",temp);

    strncpy(buf,cJSON_Print(root),size);   //将数据拷贝到buf中去

    cJSON_Delete(root);

    return 0;
}
int socket_connect(char *ip,int port)
{
    int            sockfd;
    struct sockaddr_in     serv_addr;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd < 0)
    {
        printf("create socket failure: %s\n",strerror(errno));
        return -1;
    }

    memset(&serv_addr,0,sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(port);
    inet_aton(ip,&serv_addr.sin_addr);

    if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
    {
        printf("connect to server [%s:%d] failure: %s\n", ip, port, strerror(errno));
        close(sockfd);
        return -2;
    }

    printf("connect to server [%s:%d] successfully\n",ip, port);
    return sockfd;
}
